package capitulo4;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.util.Random;

@SuppressWarnings("serial")
public class RandomPolygons extends Applet {
		private int[] xPoints = {0,-10,-7,7,10};
		private int[] yPoints = {-10,-2,10,10,-2};
		
		private int[] xPoints2 = { -6, -4, 0, 2, 4};
		private int[] yPoints2 = { -4, -6, 5, 6, 8};
	
		// Here's the shape used for drawing
		private Polygon poly;
		private Polygon poly2;
		
		//applet init event
		public void init(){
			poly= new Polygon(xPoints,yPoints,xPoints.length);	
			poly2= new Polygon(xPoints2,yPoints2,xPoints2.length);	
		} 
		
		// Applet paint event
		public void paint(Graphics g){
			// Create an instance of Graphics2D
			Graphics2D g2d = (Graphics2D)g;
			
			// save the identity transform
			AffineTransform identity = new AffineTransform();
			
			// Create a random number generator
			Random rand = new Random();
			
			// Save the window width/height
			int width = getSize().width;
			int height = getSize().height;
			
			// Fill the background with black
			g2d.setColor(Color.BLACK);
			g2d.fillRect(0, 0, width, height);
			
			for (int n=0; n<300; n++){
				// Reset Graphics2D to the identity transform
				g2d.setTransform(identity);
				
				// Move, rotate and scale the shape randomly
				g2d.translate(rand.nextInt() % width, rand.nextInt() % height);
				g2d.rotate(Math.toRadians(360 * rand.nextDouble()));
				g2d.scale(5*rand.nextDouble(), 5*rand.nextDouble());
				
				// Draw the shape wiht a random color
				g2d.setColor(new Color(rand.nextInt()));
				g2d.fill(poly);
				
				// now the other polygon
				g2d.setTransform(identity);
				
				// Move, rotate and scale the shape randomly
				g2d.translate(rand.nextInt() % width, rand.nextInt() % height);
				g2d.rotate(Math.toRadians(360 * rand.nextDouble()));
				g2d.scale(5*rand.nextDouble(), 5*rand.nextDouble());
				
				// Draw the shape wiht a random color
				g2d.setColor(new Color(rand.nextInt()));
				g2d.fill(poly2);	
			}
		}
	}
